#ifndef WORLD
#define WORLD

#include "engine.h"
#include "interface.h"
#include "soundmanager.h"

#include <pthread.h>
#include <semaphore.h>

//#define _USE_SEMAPHORE_ // if not defined use mutex

namespace sleek
{
    namespace core
    {
        namespace engine
        {
            enum e_thread_function
            {
                etf_nothink = 0,
                etf_sound = 1,
                etf_physics = 2,
                etf_event = 4,
                etf_render = 8,
                etf_all = etf_sound | etf_physics | etf_event | etf_render
            };
            static const char *e_thread_function_name[] =
            {
                "etf_nothink",
                "etf_sound",
                "etf_physics",
                "etf_event",
                "etf_render",
                "etf_all",
                0
            };
            class world
            {
                public:
                    world(device::Device*);
                    virtual ~world();

                    void lock();
                    void unlock();
                    bool isLock();
                    void waitPhysics();

                    void enableThread(int);
                    int getCurrentThread();
                    void disableThread(int);

                    engine *getEngine();
                    interface *getInterface();
                    soundmanager *getSoundManager();
                    texture::draw *getDrawManager();
                    device::Device *getDevice();

                    core::os::stringc getInitError();
                    void printInitError();

                    void render();
                protected:
                    engine *smgr;
                    interface *gui;
                    soundmanager *sdmgr;
                    device::Device *screen;
                    texture::draw *mom;
                private:
                    bool rdn, snd, phy, evt, locked, wphy;
                    #if defined _USE_SEMAPHORE_
                        sem_t mutex;
                    #else
                        pthread_mutex_t mutex;
                    #endif
                    pthread_t trdn, tsnd, tphy, tevt;

                    friend void* FunctionThreadSound  (void *data);
                    friend void* FunctionThreadPhysics(void *data);
                    friend void* FunctionThreadEvent  (void *data);
                    friend void* FunctionThreadRender (void *data);
            };
        }
    }
}

#endif // WORLD
